#!/usr/bin/env python
# -*- coding: utf8 -*-
# Filename: script/extract_result.py
# Author: xul - 294739212@qq.com
# Create: 2017-04-02 22:54:54
# Description: extract precision, recall, F1 from result.txt


def extract(xls_file):
    f = open(xls_file, 'w')
    lines = open('../result.txt').readlines()
    pre_rec_f1 = []
    cur_pos = 0
    for line in lines:
        line_list = line.split('\t:\t')
        if len(line_list) == 2:
            if line_list[1].startswith('FN: '):
                pre_rec_f1.append(line_list[0])
        elif len(line_list) == 1:
            if line_list[0].startswith('precision is: '):
                pre_rec_f1.append(line_list[0][len('precision is: '):].strip())
            elif line_list[0].startswith('recall is: '):
                pre_rec_f1.append(line_list[0][len('recall is: '):].strip())
            elif line_list[0].startswith('F1 score is: '):
                pre_rec_f1.append(line_list[0][len('F1 score is: '):].strip())

            cur_pre_rec_f1 = pre_rec_f1[cur_pos:]
            if len(cur_pre_rec_f1) == 4 and float(cur_pre_rec_f1[3]) >= 0.55:
                f.write(cur_pre_rec_f1[0] + '\t' + cur_pre_rec_f1[1] + '\t' +
                        cur_pre_rec_f1[2] + '\t' + cur_pre_rec_f1[3] + '\n')
            if len(cur_pre_rec_f1) == 4:
                cur_pos += 4
    f.close()


if __name__ == '__main__':
    xls_file = '../xls.txt'
    extract(xls_file)
